package proyecto.DataAccessObject;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import proyecto.EntityBeans.Persona;

@Stateless
@Remote(PersonaDAORemote.class)
public class PersonaDAO implements PersonaDAORemote {

	@PersistenceContext
	EntityManager entityManager;


	public int save (Persona persona){		
		try{			
			entityManager.persist(persona);			
		} 
		catch(Exception e){
			e.printStackTrace();
		}	
	
		return persona.getIdPersona();
	}
	

	public Persona findById(int id) {
		return entityManager.find(Persona.class, id);
		
	}
	
		
	public void merge(Persona persona) {
		try{						
			entityManager.merge(persona);
		} 
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public List findBySurname(String surname)
	{	     
	      return entityManager.createQuery("select p from Persona p where p.apellido = :surname").setParameter("apellido", surname).getResultList();
	}
	
	public List findAll() {		
		return entityManager.createQuery("from Persona").getResultList();
	}
	
	public List findByIdList(int id)
	   {	     
	      return entityManager.createQuery("select p from Persona p where p.idPersona = :idPersona").setParameter("idPersona", id).getResultList();
	   }
	
		
	public void remove(int idPersona){	
		entityManager.remove(entityManager.find(Persona.class, idPersona));
	}
	

	public void refresh(Persona persona){
		entityManager.refresh(persona);
	}
}
